﻿import QtQuick 2.12
import QtCharts 2.3

ChartView {
    id: view
    title: "考核分占比"
    titleColor: "white"
    antialiasing: true
    legend.labelColor:"white"
    legend.markerShape:Legend.MarkerShapeRectangle
    legend.alignment: Qt.AlignBottom

    BarCategoryAxis {
        id:axisX
        categories: ["五月", "四月", "三月", "二月", "一月"]
        labelsColor: "#13B2E8"
        gridLineColor: "#13B2E8"
        color: "#13B2E8"
    }
    ValueAxis {
        id:axisY
        min:0
        max:21
        labelsColor: "#13B2E8"
        gridLineColor: "#13B2E8"
        color: "#13B2E8"
    }

    BarSeries {
        id: barS
        axisX :axisX
        axisY: axisY
        barWidth: 0.8
        labelsPosition: AbstractBarSeries.LabelsOutsideEnd
        labelsVisible: true
    }

    Component.onCompleted: {
        let plants = query.getPlants();
        plants = JSON.parse(plants);
        for (let i = 0; i < 6; i++)
            plants.shift();
        plants.forEach(drawBar);
        setColor();
    }

    function getScore(plantName) {
        let score = query.getScore(plantName);
        return JSON.parse(score);
    }
    function drawBar(plantName) {
        let obj = getScore(plantName);
        let values = new Array;
        for (var prop in obj) {
            values.push(obj[prop].score);
        }
        barS.append(modelH.trPlantName(plantName), values);
    }
    function setColor() {
        var colors = ["#1abc9c", "#B33771", "#3498db","#9b59b6","#34495e","#f1c40f","#e67e22","#e84393","#e74c3c","#BDC581","#95a5a6"];
        let count = barS.count;
        for(let i = 0; i < count; i++) {
            barS.at(i).color = colors[i];
        }
    }
}
